package callnumber.gtdev5.com.photoVault.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;
import com.gtdev5.geetolsdk.mylibrary.util.Utils;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.bean.VideoFileBean;
import callnumber.gtdev5.com.photoVault.bean.PhotoBean;
import callnumber.gtdev5.com.photoVault.utils.ImageUtils;

/********************
 *  Created by ：  lyq 
 *         Date：  2019\1\22 0022.
 * Introduction：   
 *********************/

public class VideoImageAdapter extends RecyclerView.Adapter<VideoImageAdapter.MyViewHolder> {
    private int image_num;
    private List<Bitmap> datas;
    private Context context;
    private OnItemClickListener listener;
    private String key = "123456";
    private List<VideoFileBean> data;

    public VideoImageAdapter(Context context, int num, List<Bitmap> datas, List<VideoFileBean> data, OnItemClickListener listener) {
        this.data = data;
        this.image_num = num;
        this.datas = datas;
        this.context = context;
        this.listener = listener;
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.su_item_image_choose_layout, null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (position >= datas.size()) {
            holder.iv_del.setVisibility(View.GONE);
            holder.imageView.setImageResource(R.mipmap.white_sbg);
            holder.imageView.setOnClickListener(v -> {
//                listener.OnAddItemClick(image_num - datas.size());
            });
        } else {
            try {
                Glide.with(context).load(datas.get(position)).into(holder.imageView);
            } catch (Exception e) {
                Glide.with(context).load(datas.get(position)).into(holder.imageView);
            }

            holder.imageView.setOnClickListener(v -> {
                listener.OnItemClick(position);
            });

            holder.iv_del.setVisibility(View.VISIBLE);
            holder.iv_del.setOnClickListener(v -> {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("删除该视频?");
                builder.setMessage("您是否要删除视频?（注意！删除后无法恢复！）");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        datas.remove(position);
                        DataSupport.delete(VideoFileBean.class, data.get(position).getId());
                        if (ImageUtils.deleteFile(data.get(position).getName())) {
                            ToastUtils.showShortToast("删除成功");
                        } else
                            ToastUtils.showShortToast("删除失败");
                        notifyDataSetChanged();

                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            });

        }
    }


    @Override
    public int getItemCount() {
        return datas.size() < image_num ? datas.size() + 1 : datas.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private ImageView iv_del;

        public MyViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv);
            iv_del = itemView.findViewById(R.id.iv_del);
        }
    }

    public interface OnItemClickListener {
        void OnItemClick(int position);

//        void OnAddItemClick(int count);
    }

    /**
     * 添加数据
     *
     * @param dat
     */
    public void AddDatas(List<Bitmap> dat) {
        if (dat != null) {
            this.datas.addAll(dat);
            this.notifyDataSetChanged();
        }
    }

    /**
     * 添加单条数据
     *
     * @param string
     */
    public void AddAloneData(Bitmap string) {
        if (Utils.isNotEmpty(string.toString())) {
            this.datas.add(string);
            this.notifyItemInserted(this.datas.size() - 1);
        }
    }

    /**
     * 替换单条数据
     *
     * @param string
     * @param position
     */
    public void repeleceData(Bitmap string, int position) {
        if (Utils.isNotEmpty(string.toString()) && position < this.datas.size()) {
            this.datas.remove(position);
            this.datas.add(position, string);
            this.notifyItemChanged(position);
        }
    }

    public List<Bitmap> getDatas() {
        return datas;
    }
}
